package cn.star.jots.preview.service.impl;

import cn.star.jots.film.mapper.FilmMapper;
import cn.star.jots.preview.mapper.PreviewMapper;
import cn.star.jots.preview.pojo.dto.PreviewSaveParam;
import cn.star.jots.preview.pojo.entity.Preview;
import cn.star.jots.preview.pojo.vo.PreviewVO;
import cn.star.jots.preview.service.PreviewService;
import cn.star.jots.tag.mapper.FilmTagMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PreviewServiceImpl implements PreviewService {

    @Autowired
    PreviewMapper previewMapper;
    @Autowired
    FilmMapper filmMapper;
    @Autowired
    FilmTagMapper filmTagMapper;

    @Override
    public void save(PreviewSaveParam previewSaveParam) {
        log.debug("保存预告片信息:previewSaveParam={}", previewSaveParam);
        Preview preview = new Preview();
        BeanUtils.copyProperties(previewSaveParam, preview);
        if (previewSaveParam.getId() == null) {
            preview.setCreateTime(new Date());
            previewMapper.insert(preview);
        } else {
            log.debug("更新预告片信息:preview={}", preview);
            preview.setUpdateTime(new Date());
            previewMapper.update(preview);
        }
    }

    @Override
    public List<PreviewVO> selectByFilmId(Long filmId) {
        log.debug("查询预告片信息,filmId参数:{}", filmId);
        return previewMapper.selectByFilmId(filmId);
    }

    @Override
    public List<PreviewVO> selectById(Long id) {
        log.debug("查询预告片信息,id参数:{}", id);
        return previewMapper.selectById(id);
    }

    @Override
    public void delete(Long id) {
        log.debug("删除预告片信息,id参数:{}", id);
        previewMapper.delete(id);
    }

    @Transactional
    @Override
    public void deleteByFilmId(Long filmId) {
        log.debug("删除预告片信息,filmId参数:{}", filmId);
        previewMapper.deleteByFilmId(filmId);
        filmMapper.deleteFilm(filmId);
        filmTagMapper.deleteByFilmId(filmId);
    }

    @Override
    public List<PreviewVO> selectAll() {
        log.debug("查询预告片信息");
        return previewMapper.selectAll();
    }

}
